'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {

  async noticecreat() {
    const { ctx } = this;
    const { noticemsg } = ctx.request.body;
    if (!noticemsg) {
      return ctx.body = { success: false, msg: '请传入指定参数' };
    }
    try {
      await ctx.model.Notice.create({
        noticemsg,
        addTime: Date.now(),
      });
      ctx.body = { success: true, msg: '添加成功' };
    } catch (e) {
      ctx.body = { success: false, msg: '发生未知错误' };
    }
  }

  async noticefind() {
    const { ctx } = this;
    try {
      const res = await ctx.model.Notice.findAll({
        order: [[ 'noticeid', 'DESC' ]],
      });
      ctx.body = { success: true, data: res };
    } catch (e) {
      ctx.body = { success: false, msg: '发生未知错误' };
    }
  }

  async noticedestory() {
    const { ctx } = this;
    const { noticeid } = ctx.request.body;
    if (!noticeid) return ctx.body = { success: false, msg: '请传入指定参数' };
    try {
      await ctx.model.Notice.destroy({
        where: {
          noticeid,
        },
      });
      ctx.body = { success: true, msg: '删除成功' };
    } catch (e) {
      ctx.body = { success: false, msg: '发生未知错误' };
    }
  }


}

module.exports = HomeController;
